<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>仓库管理</title>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/icon.css"></link>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">

        var url;
        //  库房类别模块管理

        $(document).ready(function () {

            $("#tree").tree({
                url: '/admin/store/loadTreeInfo',
                onLoadSuccess: function (node, data) {
                    $("#tree").tree('expandAll');
                },
                onClick: function (node) {
                    if (node.attributes.state == 0) { // 假如是叶子节点 删除按钮恢复可用
                        $('#del').linkbutton('enable');
                    } else {
                        $('#del').linkbutton('disable');
                    }
                    $("#dg").datagrid('load', {
                        "name": $("#s_name").val(),
                        "type.id": node.id
                    });
                }
            });

        });

        function openGoodsTypeAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "新增仓库类别");
        }

        function deleteGoodsType() {
            var node = $('#tree').tree('getSelected'); // 获取选中节点
            var id = node.id;
            $.post("/admin/store/delete", {id: id}, function (result) {
                if (result.success) {
                    $('#tree').tree('reload');
                } else {
                    $.messager.alert("系统提示", "提交失败，请联系管理员！");
                }
                $('#del').linkbutton('disable');
            }, "json");
        }

        function saveGoodsType() {
            var kufangmingcheng = $("#goodsTypeName").val();
            if (!$("#fm").form("validate")) {
                return;
            }
            var node = $('#tree').tree('getSelected'); // 获取选中节点
            var parentId; // 父节点Id
            if (node == null) {
                parentId = 1;
            } else {
                parentId = node.id;
            }
            $.post("/admin/store/save", {'kufangmingcheng': kufangmingcheng, 'p_id': parentId}, function (result) {
                if (result.success) {
                    $('#tree').tree('reload');
                    closeGoodsTypeDialog();
                } else {
                    $.messager.alert("系统提示", "提交失败，请联系管理员！");
                }
            }, "json");
        }

        function closeGoodsTypeDialog() {
            $("#dlg").dialog("close");
            $("#goodsTypeName").val("");
        }

        // 商品管理模块
        function formatGoodsTypeId(val, row) {
            return row.type.id;
        }

        function formatGoodsTypeName(val, row) {
            return row.type.name;
        }

        function searchGoods() {
            $("#dg").datagrid('load', {
                "name": $("#s_name").val()
            });
        }

        function formatPurchasingPrice(val, row) {
            return "￥" + val;
        }

        function formatSellingPrice(val, row) {
            return "￥" + val;
        }

        function openGoodsAddDialog() {
            $("#dlg2").dialog("open").dialog("setTitle", "添加仓库信息");
            url = "/admin/store/save";
            var node = $('#tree').tree('getSelected'); // 获取选中节点
            if (node != null && node.id != 1) {
                $("#typeId").val(node.id);
                $("#typeName").val(node.text);
            } else {
                $("#typeId").val("");
                $("#typeName").val("");
            }
            $.post("/admin/store/genGoodsCode", {}, function (result) {
                $("#code").val(result);
            });
            $("#saveAndAddNextBuuton").show();
        }

        function openGoodsModifyDialog() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一条要修改的数据！");
                return;
            }
            var row = selectedRows[0];
            $("#dlg2").dialog("open").dialog("setTitle", "修改商品信息");
            $("#fm2").form("load", row);
            $("#typeId").val(row.type.id);
            $("#typeName").val(row.type.name);
            url = "/admin/goods/save?id=" + row.id;
            $("#saveAndAddNextBuuton").hide();
        }

        function chooseGoodsType() {
            $("#dlg3").dialog("open").dialog("setTitle", "选择商品类别");
            $("#typeTree").tree({
                url: '/admin/goodsType/loadTreeInfo',
                onLoadSuccess: function (node, data) {
                    var rootNode = $("#typeTree").tree('getRoot');
                    $("#typeTree").tree('expand', rootNode.target);
                }
            });
        }

        function saveGoodsTypeChoose() {
            var node = $('#typeTree').tree('getSelected'); // 获取选中节点
            if (node != null && node.id != 1) {
                $("#typeId").val(node.id);
                $("#typeName").val(node.text);
            }
            $("#dlg3").dialog("close");
        }

        function closeGoodsTypeChooseDialog() {
            $("#dlg3").dialog("close");
        }

        function saveGoods(type) {
            $("#fm2").form("submit", {
                url: url,
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "保存成功！");
                        resetValue();
                        if (type == 2) {
                            var node = $('#tree').tree('getSelected'); // 获取选中节点
                            if (node != null && node.id != 1) {
                                $("#typeId").val(node.id);
                                $("#typeName").val(node.text);
                            } else {
                                $("#typeId").val("");
                                $("#typeName").val("");
                            }
                            $.post("/admin/goods/genGoodsCode", {}, function (result) {
                                $("#code").val(result);
                            });
                        }
                        if (type == 1) {
                            $("#dlg2").dialog("close");
                        }
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("系统提示", result.errorInfo);
                    }
                }
            });
        }

        function resetValue() {
            $("#typeId").val("");
            $("#typeName").val("");
            $("#id").val("");
            $("#name").val("");
            $("#model").val("");
            $("#unit").combobox("setValue", "");
            $('#purchasingPrice').numberbox('setValue', "");
            $('#sellingPrice').numberbox('setValue', "");
            $('#minNum').numberbox('setValue', "");
            $("#producer").val("");
            $("#remarks").val("");
        }

        function closeGoodsDialog() {
            $("#dlg2").dialog("close");
            resetValue();
        }

        function deleteGoods() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的数据！");
                return;
            }
            var id = selectedRows[0].id;
            $.messager.confirm("系统提示", "您确定要删除这条数据吗？", function (r) {
                if (r) {
                    $.post("/admin/store/delete", {id: id}, function (result) {
                        if (result.success) {
                            $.messager.alert("系统提示", "数据已成功删除！");
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", "<font color=red>" + result.errorInfo + "</font>");
                        }
                    }, "json");
                }
            });
        }

        // 商品单位模块
        function openChooseGoodsUnitDialog() {
            $("#dlg4").dialog("open").dialog("setTitle", "单位");
        }

        function openGoodsUnitAddDialog() {
            $("#dlg5").dialog("open").dialog("setTitle", "添加单位信息");
        }

        function deleteGoodsUnit() {
            var selectedRows = $("#dg4").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择要删除的数据！");
                return;
            }
            var id = selectedRows[0].id;
            $.messager.confirm("系统提示", "您确定要删除这条数据吗？", function (r) {
                if (r) {
                    $.post("/admin/goodsUnit/delete", {id: id}, function (result) {
                        if (result.success) {
                            $("#dg4").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            });
        }

        function chooseGoodsUnit() {
            var selectedRows = $("#dg4").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择单位！");
                return;
            }
            var name = selectedRows[0].name;
            $("#unit").combobox("reload");
            $("#unit").combobox("setValue", name);
            $("#dlg4").dialog("close");
        }

        function closeGoodsUnitDialog() {
            $("#dlg4").dialog("close");
        }

        function saveGoodsUnit() {
            $("#fm5").form("submit", {
                url: "/admin/goodsUnit/save",
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "保存成功！");
                        closeGoodsUnitAddDialog();
                        $("#dg4").datagrid("reload");
                    } else {
                        $.messager.alert("系统提示", result.errorInfo);
                    }
                }
            });
        }

        function closeGoodsUnitAddDialog() {
            $("#dlg5").dialog("close");
            $("#goodsUnitName").val("");
        }

        $(function () {

            $("#dg").datagrid({
                //双击事件
                onDblClickRow: function (index, row) {
                    $("#dlg2").dialog("open").dialog("setTitle", "修改商品信息");
                    $("#fm2").form("load", row);
                    $("#typeId").val(row.type.id);
                    $("#typeName").val(row.type.name);
                    url = "/admin/store/save?id=" + row.id;
                    $("#saveAndAddNextBuuton").hide();
                }
            });

            $("#dg4").datagrid({
                //双击事件
                onDblClickRow: function (index, row) {
                    var selectedRows = $("#dg4").datagrid("getSelections");
                    var name = selectedRows[0].name;
                    $("#unit").combobox("reload");
                    $("#unit").combobox("setValue", name);
                    $("#dlg4").dialog("close");
                }
            });
        });

    </script>
</head>
<body class="easyui-layout" style="padding-top: 2px">
<div data-options="region:'west',collapsible:false" style="width:200px" title="仓库类别" split=true>
    <ul id="tree" class="easyui-tree" style="padding: 10px"></ul>
    <div style="position:absolute;bottom: 10px;left: 15px">
        <a href="javascript:openGoodsTypeAddDialog()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">新增</a>
        <a id="del" href="javascript:deleteGoodsType()" class="easyui-linkbutton"
           data-options="iconCls:'icon-remove',disabled:true">删除</a>
    </div>
</div>

<div data-options="region:'center'">
    <table id="dg" title="仓库管理" class="easyui-datagrid"
           fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
           url="/admin/goods/list" fit="true" toolbar="#tb">
        <thead>
        <tr>
            <th field="id" width="20" align="center">编号</th>
            <th field="kufangmingcheng" width="30" align="center">仓库名称</th>
            <th field="createtime" width="150" align="center">创建时间</th>
            <!--<th field="typeId" width="40" align="center" formatter="formatGoodsTypeId" hidden=true>类别ID</th>-->
        </tr>
        </thead>
    </table>
    <div id="tb">
        <div>
            <a href="javascript:openGoodsAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
            <a href="javascript:openGoodsModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit"
               plain="true">修改</a>
            <a href="javascript:deleteGoods()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
        </div>
        <div>
            &nbsp;仓库名称：&nbsp;<input type="text" id="s_name" size="20" onkeydown="if(event.keyCode==13) searchGoods()"/>
            <a href="javascript:searchGoods()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
        </div>
    </div>
</div>

<div id="dlg" class="easyui-dialog" style="width:300px;height:120px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons" data-options="onClose:function(){$('#goodsTypeName').val('');}">

    <form id="fm" method="post">
        <div>
            仓库类别：&nbsp;<input type="text" id="kufangType" name="name" class="easyui-validatebox" required="true"/>
        </div>
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:saveGoodsType()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeGoodsTypeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>

<div id="dlg2" class="easyui-dialog" style="width: 500px;height:150px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons2" data-options="onClose:function(){resetValue()}">
    <form id="fm2" method="post">
        <table cellspacing="8px">
            <tr>
                <td>仓库名称：</td>
                <td colspan="4">
                    <input type="text" id="name" name="kufangmingcheng" class="easyui-validatebox" required="true"
                           style="width: 350px;"/>
                </td>
            </tr>
            <!--<tr>-->
            <!--<td valign="top">备注：</td>-->
            <!--<td colspan="4">-->
            <!--<textarea rows="5" cols="47" id="remarks" name="beizhu"></textarea>-->
            <!--</td>-->
            <!--</tr>-->
        </table>
    </form>
</div>

<div id="dlg-buttons2">
    <a id="saveAndAddNextBuuton" href="javascript:saveGoods(2)" class="easyui-linkbutton"
       iconCls="icon-ok">保存并新增下一仓库</a>
    <a href="javascript:saveGoods(1)" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeGoodsDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>

<div id="dlg3" class="easyui-dialog" style="width:180px;height:300px;padding: 10px 10px"
     closed="true" buttons="#dlg-buttons3">

    <ul id="typeTree" class="easyui-tree"></ul>

</div>

<div id="dlg-buttons3">
    <a href="javascript:saveGoodsTypeChoose()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">选择</a>
    <a href="javascript:closeGoodsTypeChooseDialog()" class="easyui-linkbutton" iconCls="icon-cancel"
       plain="true">关闭</a>
</div>

<div id="dlg4" class="easyui-dialog" style="width:200px;height:300px;"
     closed="true">

    <table id="dg4" class="easyui-datagrid" rownumbers="true" singleSelect="true"
           url="/admin/goodsUnit/listAll" fitColumns="true" fit="true" toolbar="#tb2">
        <thead>
        <tr>
            <th field="id" width="100" align="center" hidden=true>单位ID</th>
            <th field="name" width="100" align="center">单位名称</th>
        </tr>
        </thead>
    </table>
    <div id="tb2">
        <div>
            <a href="javascript:openGoodsUnitAddDialog()" class="easyui-linkbutton" plain="true">添加</a>
            <a href="javascript:deleteGoodsUnit()" class="easyui-linkbutton" plain="true">删除</a>
            <a href="javascript:chooseGoodsUnit()" class="easyui-linkbutton" plain="true">确定</a>
            <a href="javascript:closeGoodsUnitDialog()" class="easyui-linkbutton" plain="true">取消</a>
        </div>
    </div>

</div>

<div id="dlg5" class="easyui-dialog" style="width:300px;height:120px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons5" data-options="onClose:function(){$('#goodsUnitName').val('');}">

    <form id="fm5" method="post">
        <div>
            商品单位：&nbsp;<input type="text" id="goodsUnitName" name="name" class="easyui-validatebox" required="true"/>
        </div>

    </form>
</div>

<div id="dlg-buttons5">
    <a href="javascript:saveGoodsUnit()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeGoodsUnitAddDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>

</body>
</html>